package jsyf.oa.idao;

import com.alibaba.fastjson.JSONObject;
import jsyf.com.dao.BaseJdbcDao;
import jsyf.oa.ientity.IBaseEntity;

import java.util.*;

/**
 * Created by Lenovo on 2017/12/8.
 */
public abstract class BaseDao extends BaseJdbcDao implements IBaseDao {

    public IBaseEntity iBaseEntity;

    public Integer add(IBaseEntity entity) {
        entity.setId(super.generateKey());
        entity.setCreateTime(super.getCurrentTime());
        entity.setUpdateTime(super.getCurrentTime());
        return super.insert(entity.getTableName(), entity.getJSONObject());
    }

    public Integer update(IBaseEntity entity) {
        entity.setUpdateTime(super.getCurrentTime());
        return super.update(entity.getTableName(), entity.getJSONObject(), entity.getJSONObjectId());
    }

    public List<JSONObject> getJSONListByPage(String condition, Object[] params, int requestPage, int pageSize) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT ");
        JSONObject data = iBaseEntity.getJSONColumns();
        Set<Map.Entry<String, Object>> set = data.entrySet();
        for (Iterator<Map.Entry<String, Object>> iterator = set.iterator(); iterator.hasNext(); ) {
            Map.Entry<String, Object> entry = iterator.next();
            sql.append(entry.getKey() + " AS " + entry.getValue() + ",");
        }
        sql.delete(sql.length() - 1, sql.length());
        sql.append(" FROM ");
        sql.append(iBaseEntity.getTableName());
        sql.append(" WHERE 1 = 1 ");
        sql.append(condition);

        super.appendPageSql(sql, requestPage, pageSize);

        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(), params);

        return jsonList;
    }

    public Integer getPageCount(String condition, Object[] params) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT COUNT(1) AS count FROM " + iBaseEntity.getTableName() + " WHERE 1 = 1 ");
        sql.append(condition);
        String count = super.queryForString(sql.toString(), params);
        return Integer.parseInt(count);
    }


    public List<JSONObject> getJSONListByCondition(String condition, Object[] params) {
        return this.getJSONListByPage(condition, params, 1, Integer.MAX_VALUE);
    }

    public JSONObject getJSONById(String id) {
        List<JSONObject> jsonList = this.getJSONListByCondition(" AND ID = ?", new Object[]{id});
        if (jsonList != null && jsonList.size() > 0) {
            return jsonList.get(0);
        } else {
            return null;
        }
    }


    public List<IBaseEntity> getIBaseEntityListByPage(String condition, Object[] params, int requestPage, int pageSize) {
        List<JSONObject> jsonList = this.getJSONListByPage(condition, params, requestPage, pageSize);
        List<IBaseEntity> entityList = new ArrayList<>();
        if (jsonList != null && jsonList.size() > 0) {
            for (int i = 0; i < jsonList.size(); i++) {
                IBaseEntity iBaseEntity = convertJson2Entity(jsonList.get(i));
                entityList.add(iBaseEntity);
            }
        }
        return entityList;
    }

    public List<IBaseEntity> getIBaseEntityByCondition(String condition, Object[] params) {
        return this.getIBaseEntityListByPage(condition, params, 1, Integer.MAX_VALUE);
    }

    public IBaseEntity getIBaseEntityById(String id) {
        List<IBaseEntity> entityList = this.getIBaseEntityByCondition(" AND ID = ?", new Object[]{id});
        if (entityList != null && entityList.size() > 0) {
            return entityList.get(0);
        } else {
            return null;
        }
    }

    public Integer removeByCondition(String condition, String[] params) {
        StringBuffer sql = new StringBuffer();
        sql.append(" DELETE FROM " + iBaseEntity.getTableName() + " WHERE 1 = 1 ");
        sql.append(condition);
        return super.delete(sql.toString(), params);
    }

    public Integer removeById(String id) {
        return this.removeByCondition(" AND ID = ? ", new String[]{id});
    }

    public Integer delByCondition(String condition, Object[] params) {
        Integer rows = 0;
        List<IBaseEntity> entityList = this.getIBaseEntityByCondition(condition, params);
        for (int i = 0; i < entityList.size(); i++) {
            IBaseEntity iBaseEntity = entityList.get(i);
            iBaseEntity.setDelFlag(1);
            this.update(iBaseEntity);
            rows++;
        }
        return rows;
    }

    public Integer delById(String id) {
        return this.delByCondition(" AND ID = ? ", new Object[]{id});
    }

    public List<JSONObject> getViewJSONListByPage(String condition, Object[] params, int requestPage, int pageSize) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT * FROM ");
        sql.append(iBaseEntity.getViewName());
        sql.append(" WHERE 1 = 1 ");
        sql.append(condition);

        super.appendPageSql(sql, requestPage, pageSize);

        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(), params);

        return jsonList;
    }

    public Integer getViewPageCount(String condition, Object[] params) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT COUNT(1) AS count FROM " + iBaseEntity.getViewName() + " WHERE 1 = 1 ");
        sql.append(condition);
        String count = super.queryForString(sql.toString(), params);
        if (count == null) {
            count = "0";
        }
        return Integer.parseInt(count);
    }

    public List<JSONObject> getViewJSONListByCondition(String condition, Object[] params) {
        return this.getViewJSONListByPage(condition, params, 1, Integer.MAX_VALUE);
    }

    public JSONObject getViewJSONById(String id) {
        List<JSONObject> jsonList = this.getViewJSONListByCondition(" AND ID = ?", new Object[]{id});
        if (jsonList != null && jsonList.size() > 0) {
            return jsonList.get(0);
        } else {
            return null;
        }
    }

}
